/*
   Copyright (c) 2011, 2012 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-18 Marco Riccardi - initial implementation
 *  2012-04-06 Marco Riccardi - added message in DliParseException
 *
 */
package org.mizartools.dli;

public class ConditionalRegistration extends ItemDefinition {

	private Loci loci = null;
	private Antecedent antecedent = null;
	private Type type = null;
	private Cluster cluster = null;
	
	private ConditionalRegistration(){
		super(null);
	}

    public ConditionalRegistration(Loci loci, Antecedent antecedent, Type type, Cluster cluster) throws DliException {
    	super(null);
		if (loci == null){
			throw new DliException();
		}
		this.loci = loci;
		this.antecedent = antecedent;
		this.type = type;
		this.cluster = cluster;
    }
	
	public static ItemDefinition newInstance(String string) throws DliParseException {
		ConditionalRegistration conditionalRegistration = new ConditionalRegistration();
		int position = string.indexOf("(");
		if (position == -1) {
			throw new DliParseException("["+string+"] is not valid in ConditionalRegistration");
		}
		if (!string.substring(0, position).equals("condreg")) {
			throw new DliParseException("["+string+"] is not valid in ConditionalRegistration");
		}
		string = string.substring(position+1, string.length()-1);
		int positionComma = Utility.searchSeparationComma(string);
		if (positionComma == -1) {
			throw new DliParseException("["+string+"] is not valid in ConditionalRegistration");
		}
		String type = null;
		while (positionComma != -1) {
			position = string.indexOf("(");
			int positionCommaTest = string.indexOf(",");
			if (positionCommaTest != 0 && positionComma<position){
				position = positionCommaTest;
			}
			if (position == -1) {
				throw new DliParseException("["+string+"] is not valid in ConditionalRegistration");
			}
			type = string.substring(0, position);
			if (type.equals("$loci")){
				conditionalRegistration.loci = Loci.newInstance(string.substring(0, positionComma));
			} else if (type.equals("$antecedent")){
				conditionalRegistration.antecedent = Antecedent.newInstance(string.substring(0, positionComma));
			} else if (type.equals("$cluster")){
				conditionalRegistration.cluster = Cluster.newInstance(string.substring(0, positionComma));
			} else if (type.equals("$type")){
				conditionalRegistration.type = Type.newInstance(string.substring(0, positionComma));
			} else {
				throw new DliParseException("["+string+"] is not valid in ConditionalRegistration");
			}
			string = string.substring(positionComma+1, string.length());
			positionComma = Utility.searchSeparationComma(string);
		}
		position = string.indexOf("(");
		type = string.substring(0, position);
		if (type.equals("$loci")){
			conditionalRegistration.loci = Loci.newInstance(string);
		} else if (type.equals("$antecedent")){
			conditionalRegistration.antecedent = Antecedent.newInstance(string);
		} else if (type.equals("$cluster")){
			conditionalRegistration.cluster = Cluster.newInstance(string);
		} else if (type.equals("$type")){
			conditionalRegistration.type = Type.newInstance(string);
		} else {
			throw new DliParseException("["+string+"] is not valid in ConditionalRegistration");
		}
		return conditionalRegistration;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("condreg(");
		sb.append(loci.toString());
		if (antecedent != null){
			sb.append(",");
			sb.append(antecedent.toString());
		}
		if (type != null){
			sb.append(",");
			sb.append(type.toString());
		}
		if (cluster != null){
			sb.append(",");
			sb.append(cluster.toString());
		}
		sb.append(")");
		return sb.toString();
		
	}
	
}
